
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>享元模式 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../../../../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../../../../../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../../../../../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../../../../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../../../../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../../../../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../../../../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../compositemode/" />
    
    
    <link rel="prev" href="../facademode/" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../../../../../">
            
                <a href="../../../../../../">
            
                    
                    说明
            
                </a>
            

            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../../../../../">
            
                <a href="../../../../../">
            
                    
                    设计模式
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1.1" >
            
                <span>
            
                    
                    创建型模式
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1.1.1" >
            
                <span>
            
                    
                    简单工厂模式
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.1.2" >
            
                <span>
            
                    
                    工厂方法模式
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.1.3" data-path="../../createdmodel/abstractfactorymode/">
            
                <a href="../../createdmodel/abstractfactorymode/">
            
                    
                    抽象工厂模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.1.4" data-path="../../createdmodel/bulidermode/">
            
                <a href="../../createdmodel/bulidermode/">
            
                    
                    建造者模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.1.5" data-path="../../createdmodel/prototypemode/">
            
                <a href="../../createdmodel/prototypemode/">
            
                    
                    原型模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.1.6" data-path="../../createdmodel/singletonmode/">
            
                <a href="../../createdmodel/singletonmode/">
            
                    
                    单例模式
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.1.2" data-path="../">
            
                <a href="../">
            
                    
                    结构型模式
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1.2.1" data-path="../proxymode/">
            
                <a href="../proxymode/">
            
                    
                    代理模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2.2" data-path="../adaptermode/">
            
                <a href="../adaptermode/">
            
                    
                    适配器模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2.3" data-path="../bridgemode/">
            
                <a href="../bridgemode/">
            
                    
                    桥接模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2.4" data-path="../decoratormode/">
            
                <a href="../decoratormode/">
            
                    
                    装饰模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2.5" data-path="../facademode/">
            
                <a href="../facademode/">
            
                    
                    外观模式
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="2.1.2.6" data-path="./">
            
                <a href="./">
            
                    
                    享元模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2.7" data-path="../compositemode/">
            
                <a href="../compositemode/">
            
                    
                    组合模式
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.1.3" data-path="../../behavioralmodel/">
            
                <a href="../../behavioralmodel/">
            
                    
                    行为型模式
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1.3.1" data-path="../../behavioralmodel/templatemethodmode/">
            
                <a href="../../behavioralmodel/templatemethodmode/">
            
                    
                    模板方法模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3.2" data-path="../../behavioralmodel/strategymode/">
            
                <a href="../../behavioralmodel/strategymode/">
            
                    
                    策略模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3.3" data-path="../../behavioralmodel/commandmode/">
            
                <a href="../../behavioralmodel/commandmode/">
            
                    
                    命令模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3.4" data-path="../../behavioralmodel/chainofresponsibility/">
            
                <a href="../../behavioralmodel/chainofresponsibility/">
            
                    
                    责任链模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3.5" data-path="../../behavioralmodel/statemode/">
            
                <a href="../../behavioralmodel/statemode/">
            
                    
                    状态模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3.6" data-path="../../behavioralmodel/observermode/">
            
                <a href="../../behavioralmodel/observermode/">
            
                    
                    观察者模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3.7" data-path="../../behavioralmodel/mediatormode/">
            
                <a href="../../behavioralmodel/mediatormode/">
            
                    
                    中介者模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3.8" data-path="../../behavioralmodel/iteratormode/">
            
                <a href="../../behavioralmodel/iteratormode/">
            
                    
                    迭代器模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3.9" data-path="../../behavioralmodel/visitormode/">
            
                <a href="../../behavioralmodel/visitormode/">
            
                    
                    访问者模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3.10" data-path="../../behavioralmodel/mementomode/">
            
                <a href="../../behavioralmodel/mementomode/">
            
                    
                    备忘录模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3.11" data-path="../../behavioralmodel/interpretermode/">
            
                <a href="../../behavioralmodel/interpretermode/">
            
                    
                    解释器模式
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../../../../../../Nacos/">
            
                <a href="../../../../../../Nacos/">
            
                    
                    Nacos
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1.1" data-path="../../../../../../Nacos/Nacos-Introduction.html">
            
                <a href="../../../../../../Nacos/Nacos-Introduction.html">
            
                    
                    Nacos介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.2" data-path="../../../../../../Nacos/Nacos-Registration.html">
            
                <a href="../../../../../../Nacos/Nacos-Registration.html">
            
                    
                    Nacos注册中心
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.3" data-path="../../../../../../Nacos/Nacos-OpenFeign.html">
            
                <a href="../../../../../../Nacos/Nacos-OpenFeign.html">
            
                    
                    Nacos+OpenFeign
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../../../../../../Sortings/">
            
                <a href="../../../../../../Sortings/">
            
                    
                    排序算法
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1.1" data-path="../../../../../../Sortings/src/main/java/com/larscheng/www/InsertionSort/">
            
                <a href="../../../../../../Sortings/src/main/java/com/larscheng/www/InsertionSort/">
            
                    
                    插入排序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.2" data-path="../../../../../../Sortings/src/main/java/com/larscheng/www/shellSort/">
            
                <a href="../../../../../../Sortings/src/main/java/com/larscheng/www/shellSort/">
            
                    
                    希尔排序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.3" >
            
                <span>
            
                    
                    选择排序
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.4" >
            
                <span>
            
                    
                    堆排序
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.5" >
            
                <span>
            
                    
                    希尔排序
            
                </span>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../../../../../.." >享元模式</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h3 id="&#x4EAB;&#x5143;&#x6A21;&#x5F0F;">&#x4EAB;&#x5143;&#x6A21;&#x5F0F;</h3>
<p>&#x8FD0;&#x7528;&#x5171;&#x4EAB;&#x6280;&#x672F;&#x6765;&#x6709;&#x6548;&#x7684;&#x652F;&#x6301;&#x5927;&#x91CF;&#x7EC6;&#x7C92;&#x5EA6;&#x5BF9;&#x8C61;&#x7684;&#x590D;&#x7528;&#x3002;</p>
<p>&#x5B83;&#x901A;&#x8FC7;&#x5171;&#x4EAB;&#x5DF2;&#x7ECF;&#x5B58;&#x5728;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x6765;&#x5927;&#x5E45;&#x5EA6;&#x51CF;&#x5C11;&#x9700;&#x8981;&#x521B;&#x5EFA;&#x7684;&#x5BF9;&#x8C61;&#x6570;&#x91CF;&#x3001;&#x907F;&#x514D;&#x5927;&#x91CF;&#x76F8;&#x4F3C;&#x7C7B;&#x7684;&#x5F00;&#x9500;&#xFF0C;&#x4ECE;&#x800C;&#x63D0;&#x9AD8;&#x7CFB;&#x7EDF;&#x8D44;&#x6E90;&#x5229;&#x7528;&#x7387;</p>
<p>&#x5728;&#x9762;&#x5411;&#x5BF9;&#x8C61;&#x7A0B;&#x5E8F;&#x8BBE;&#x8BA1;&#x4E2D;&#xFF0C;&#x6709;&#x65F6;&#x5019;&#x4F1A;&#x9762;&#x4E34;&#x521B;&#x5EFA;&#x5927;&#x91CF;&#x76F8;&#x4F3C;&#x6216;&#x76F8;&#x540C;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x8FD9;&#x662F;&#x4F1A;&#x8017;&#x8D39;&#x5927;&#x91CF;&#x7684;&#x7CFB;&#x7EDF;&#x8D44;&#x6E90;&#x3002;&#x90A3;&#x4E48;&#x80FD;&#x5426;&#x5C06;&#x8FD9;&#x4E9B;&#x76F8;&#x540C;&#x76F8;&#x4F3C;&#x7684;&#x90E8;&#x5206;&#x8FDB;&#x884C;&#x5171;&#x4EAB;&#xFF0C;&#x907F;&#x514D;&#x91CD;&#x590D;&#x521B;&#x5EFA;&#x76F8;&#x540C;&#x72EC;&#x4EAB;&#x5B9E;&#x4F8B;</p>
<p>&#x4EAB;&#x5143;&#x6A21;&#x5F0F;&#x5206;&#x4E3A;&#x4E24;&#x79CD;&#x72B6;&#x6001;</p>
<ul>
<li>&#x5185;&#x90E8;&#x72B6;&#x6001;&#xFF1A;&#x4E0D;&#x4F1A;&#x968F;&#x7740;&#x73AF;&#x5883;&#x7684;&#x53D8;&#x5316;&#x800C;&#x6539;&#x53D8;&#x53EF;&#x5171;&#x4EAB;&#x90E8;&#x5206;</li>
<li>&#x5916;&#x90E8;&#x72B6;&#x6001;&#xFF1A;&#x968F;&#x73AF;&#x5883;&#x6539;&#x53D8;&#x800C;&#x6539;&#x53D8;&#x7684;&#x4E0D;&#x53EF;&#x4EE5;&#x5171;&#x4EAB;&#x7684;&#x90E8;&#x5206;&#x3002;&#x4EAB;&#x5143;&#x6A21;&#x5F0F;&#x7684;&#x5B9E;&#x73B0;&#x8981;&#x9886;&#x5C31;&#x662F;&#x533A;&#x5206;&#x4E24;&#x79CD;&#x72B6;&#x6001;&#x5E76;&#x5C06;&#x5916;&#x90E8;&#x72B6;&#x6001;&#x5916;&#x90E8;&#x5316;</li>
</ul>
<h3 id="&#x4F18;&#x7F3A;&#x70B9;">&#x4F18;&#x7F3A;&#x70B9;</h3>
<p>&#x4F18;&#x70B9;&#xFF1A;</p>
<p>&#x76F8;&#x540C;&#x5BF9;&#x8C61;&#x53EA;&#x4FDD;&#x5B58;&#x4E00;&#x5BF8;&#xFF0C;&#x51CF;&#x5C11;&#x5BF9;&#x8C61;&#x6570;&#x91CF;&#xFF0C;&#x964D;&#x4F4E;&#x7CFB;&#x7EDF;&#x4E2D;&#x7EC6;&#x7C92;&#x5EA6;&#x5BF9;&#x8C61;&#x7ED9;&#x5185;&#x5B58;&#x5E26;&#x6765;&#x7684;&#x538B;&#x529B;</p>
<p>&#x7F3A;&#x70B9;&#xFF1A;</p>
<p>&#x589E;&#x52A0;&#x7A0B;&#x5E8F;&#x590D;&#x6742;&#x6027;<br>&#x7A0B;&#x5E8F;&#x542F;&#x52A8;&#x65F6;&#x95F4;&#x53D8;&#x957F;&#xFF0C;&#x56E0;&#x4E3A;&#x8981;&#x8BFB;&#x53D6;&#x5916;&#x90E8;&#x72B6;&#x6001;</p>
<h3 id="&#x89D2;&#x8272;">&#x89D2;&#x8272;</h3>
<ul>
<li>&#x62BD;&#x8C61;&#x4EAB;&#x5143;&#x89D2;&#x8272;&#xFF1A;&#x662F;&#x6240;&#x6709;&#x4EAB;&#x5143;&#x7C7B;&#x7684;&#x57FA;&#x7C7B;&#xFF0C;&#x4E3A;&#x5177;&#x4F53;&#x4EAB;&#x5143;&#x89C4;&#x8303;&#x9700;&#x8981;&#x5B9E;&#x73B0;&#x7684;&#x516C;&#x5171;&#x63A5;&#x53E3;&#xFF0C;&#x975E;&#x4EAB;&#x5143;&#x7684;&#x5916;&#x90E8;&#x72B6;&#x6001;&#x4EE5;&#x53C2;&#x6570;&#x7684;&#x5F62;&#x5F0F;&#x901A;&#x8FC7;&#x65B9;&#x6CD5;&#x4F20;&#x5165;</li>
<li>&#x5177;&#x4F53;&#x4EAB;&#x5143;&#x89D2;&#x8272;&#xFF1A;&#x5B9E;&#x73B0;&#x62BD;&#x8C61;&#x4EAB;&#x5143;&#x89D2;&#x8272;</li>
<li>&#x975E;&#x4EAB;&#x5143;&#x89D2;&#x8272;&#xFF1A;&#x662F;&#x4E0D;&#x53EF;&#x4EE5;&#x5171;&#x4EAB;&#x7684;&#x5916;&#x90E8;&#x72B6;&#x6001;&#xFF0C;&#x5B83;&#x4EE5;&#x53C2;&#x6570;&#x7684;&#x5F62;&#x5F0F;&#x6CE8;&#x5165;&#x5177;&#x4F53;&#x4EAB;&#x5143;&#x7684;&#x76F8;&#x5173;&#x65B9;&#x6CD5;&#x4E2D;</li>
<li>&#x4EAB;&#x5143;&#x5DE5;&#x5382;&#x89D2;&#x8272;&#xFF1A;&#x8D1F;&#x8D23;&#x521B;&#x5EFA;&#x548C;&#x7BA1;&#x7406;&#x4EAB;&#x5143;&#x89D2;&#x8272;&#x3002;&#x5F53;&#x5BA2;&#x6237;&#x5BF9;&#x8C61;&#x8BF7;&#x6C42;&#x4E00;&#x4E2A;&#x4EAB;&#x5143;&#x5BF9;&#x8C61;&#x65F6;&#xFF0C;&#x4EAB;&#x5143;&#x5DE5;&#x5382;&#x68C0;&#x67E5;&#x7CFB;&#x7EDF;&#x4E2D;&#x662F;&#x5426;&#x5B58;&#x5728;&#x7B26;&#x5408;&#x8981;&#x6C42;&#x7684;&#x4EAB;&#x5143;&#x5BF9;&#x8C61;&#xFF0C;&#x5982;&#x679C;&#x5B58;&#x5728;&#x5219;&#x63D0;&#x4F9B;&#x7ED9;&#x5BA2;&#x6237;&#xFF0C;&#x5982;&#x679C;&#x4E0D;&#x5B58;&#x5728;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x4EAB;&#x5143;&#x5BF9;&#x8C61;</li>
</ul>
<p><img src="http://c.biancheng.net/uploads/allimg/181115/3-1Q115161342242.gif" alt=""></p>
<h3 id="&#x4EE3;&#x7801;">&#x4EE3;&#x7801;</h3>
<pre><code class="lang-java">
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">FlyWightTest</span> </span>{

    <span class="hljs-comment">/**
     * &#x975E;&#x4EAB;&#x5143;&#x89D2;&#x8272;&#xFF1A;&#x5305;&#x542B;&#x975E;&#x5171;&#x4EAB;&#x7684;&#x5916;&#x90E8;&#x72B6;&#x6001;&#x4FE1;&#x606F;info&#xFF0C;
     */</span>
    <span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UnsharedConcreteFlyweight</span></span>{
        <span class="hljs-keyword">private</span> String info;

        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">UnsharedConcreteFlyweight</span><span class="hljs-params">(String info)</span> </span>{
            <span class="hljs-keyword">this</span>.info = info;
        }

        <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getInfo</span><span class="hljs-params">()</span> </span>{
            <span class="hljs-keyword">return</span> info;
        }

        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setInfo</span><span class="hljs-params">(String info)</span> </span>{
            <span class="hljs-keyword">this</span>.info = info;
        }
    }

    <span class="hljs-comment">/**
     * &#x62BD;&#x8C61;&#x4EAB;&#x5143;&#x89D2;&#x8272;&#xFF0C;&#x975E;&#x4EAB;&#x5143;&#x7684;&#x5916;&#x90E8;&#x72B6;&#x6001;&#x4E00;&#x53C2;&#x6570;&#x7684;&#x5F62;&#x5F0F;&#x901A;&#x8FC7;&#x8BE5;&#x65B9;&#x6CD5;&#x4F20;&#x5165;
     */</span>
    <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">FlyWeight</span></span>{
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">operation</span><span class="hljs-params">(UnsharedConcreteFlyweight state)</span></span>;
    }

    <span class="hljs-comment">/**
     * &#x5177;&#x4F53;&#x4EAB;&#x5143;&#x89D2;&#x8272;&#xFF0C;&#x5305;&#x542B;&#x5173;&#x952E;&#x5B57;key&#xFF0C;
     */</span>
    <span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ConcreteFlyweight</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">FlyWeight</span></span>{

        <span class="hljs-keyword">private</span> String key;

        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">ConcreteFlyweight</span><span class="hljs-params">(String key)</span> </span>{
            <span class="hljs-keyword">this</span>.key = key;
            System.out.println(<span class="hljs-string">&quot;&#x5177;&#x4F53;&#x4EAB;&#x5143;&quot;</span>+key+<span class="hljs-string">&quot;&#x88AB;&#x521B;&#x5EFA;&#xFF01;&quot;</span>);
        }

        <span class="hljs-meta">@Override</span>
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">operation</span><span class="hljs-params">(UnsharedConcreteFlyweight state)</span> </span>{
            System.out.print(<span class="hljs-string">&quot;&#x5177;&#x4F53;&#x4EAB;&#x5143;&quot;</span>+key+<span class="hljs-string">&quot;&#x88AB;&#x521B;&#x5EFA;&#xFF01;&quot;</span>);
            System.out.println(<span class="hljs-string">&quot;&#x975E;&#x4EAB;&#x5143;&#x4FE1;&#x606F;&#x662F;&#xFF1A;&quot;</span>+state.getInfo());
        }


    }
    <span class="hljs-comment">/**
     * &#x4EAB;&#x5143;&#x5DE5;&#x5382;&#x89D2;&#x8272;&#xFF0C;&#x4F7F;&#x7528;&#x5173;&#x952E;&#x5B57;key&#x7BA1;&#x7406;&#x5177;&#x4F53;&#x4EAB;&#x5143;&#x89D2;&#x8272;&#xFF0C;&#x6709;&#x5219;&#x8FD4;&#x56DE;&#xFF0C;&#x65E0;&#x5219;&#x521B;&#x5EFA;
     */</span>
    <span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">FlyweightFactory</span></span>{
        <span class="hljs-keyword">private</span> HashMap&lt;String,FlyWeight&gt; flyWeightHashMap = <span class="hljs-keyword">new</span> HashMap&lt;String, FlyWeight&gt;();

        <span class="hljs-function"><span class="hljs-keyword">public</span> FlyWeight <span class="hljs-title">getFlyWeight</span><span class="hljs-params">(String key)</span></span>{
            FlyWeight flyWeight = (FlyWeight)flyWeightHashMap.get(key);
            <span class="hljs-keyword">if</span> (flyWeight!=<span class="hljs-keyword">null</span>){
                System.out.println(<span class="hljs-string">&quot;&#x5177;&#x4F53;&#x4EAB;&#x5143;&quot;</span>+key+<span class="hljs-string">&quot;&#x5DF2;&#x5B58;&#x5728;&#xFF0C;&#x88AB;&#x6210;&#x529F;&#x83B7;&#x53D6;&quot;</span>);
            }<span class="hljs-keyword">else</span> {
                flyWeight = <span class="hljs-keyword">new</span> ConcreteFlyweight(key);
                flyWeightHashMap.put(key,flyWeight);
            }
            <span class="hljs-keyword">return</span> flyWeight;
        }
    }

    <span class="hljs-comment">/**
     * &#x5BA2;&#x6237;&#x89D2;&#x8272;&#xFF1A;&#x901A;&#x8FC7;&#x4EAB;&#x5143;&#x5DE5;&#x5382;&#x83B7;&#x53D6;&#x5177;&#x4F53;&#x4EAB;&#x5143;&#xFF0C;&#x5E76;&#x8BBF;&#x95EE;&#x5177;&#x4F53;&#x4EAB;&#x5143;&#x7684;&#x76F8;&#x5173;&#x65B9;&#x6CD5;
     * <span class="hljs-doctag">@param</span> args
     */</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
        FlyweightFactory factory = <span class="hljs-keyword">new</span> FlyweightFactory();
        FlyWeight flyWeight1 = factory.getFlyWeight(<span class="hljs-string">&quot;a&quot;</span>);
        FlyWeight flyWeight2 = factory.getFlyWeight(<span class="hljs-string">&quot;b&quot;</span>);
        FlyWeight flyWeight3 = factory.getFlyWeight(<span class="hljs-string">&quot;c&quot;</span>);
        FlyWeight flyWeight4 = factory.getFlyWeight(<span class="hljs-string">&quot;a&quot;</span>);
        FlyWeight flyWeight5 = factory.getFlyWeight(<span class="hljs-string">&quot;b&quot;</span>);

        System.out.println(<span class="hljs-string">&quot;----------------------------------------------&quot;</span>);

        flyWeight1.operation(<span class="hljs-keyword">new</span> UnsharedConcreteFlyweight(<span class="hljs-string">&quot;1-a&quot;</span>));
        flyWeight2.operation(<span class="hljs-keyword">new</span> UnsharedConcreteFlyweight(<span class="hljs-string">&quot;1-b&quot;</span>));
        flyWeight3.operation(<span class="hljs-keyword">new</span> UnsharedConcreteFlyweight(<span class="hljs-string">&quot;1-c&quot;</span>));
        flyWeight4.operation(<span class="hljs-keyword">new</span> UnsharedConcreteFlyweight(<span class="hljs-string">&quot;2-a&quot;</span>));
        flyWeight5.operation(<span class="hljs-keyword">new</span> UnsharedConcreteFlyweight(<span class="hljs-string">&quot;2-b&quot;</span>));
    }
}
</code></pre>
<h3 id="&#x6A21;&#x62DF;&#x573A;&#x666F;">&#x6A21;&#x62DF;&#x573A;&#x666F;</h3>
<p>&#x4E94;&#x5B50;&#x68CB;&#x4E0E;&#x56F4;&#x68CB;&#x4E00;&#x6837;&#xFF0C;&#x5305;&#x542B;&#x591A;&#x4E2A;&quot;&#x9ED1;&quot; &#x6216;&#x8005; &quot;&#x767D;&quot; &#x8272;&#x7684;&#x68CB;&#x5B50;&#xFF0C;&#x90A3;&#x4E48;&#x4E94;&#x5B50;&#x68CB;&#x7684;&#x68CB;&#x5B50;&#x5373;&#x53EF;&#x4EE5;&#x7528;&#x6765;&#x73A9;&#x56F4;&#x68CB;&#x4E5F;&#x53EF;&#x4EE5;&#x4E0B;&#x4E94;&#x5B50;&#x68CB;&#x7528;&#xFF0C;&#x4EAB;&#x5143;&#x6A21;&#x5F0F;&#x5C31;&#x53EF;&#x4EE5;&#x7528;&#x4E0A;&#x6765;&#x63CF;&#x8FF0;&#x8FD9;&#x4E2A;&#x573A;&#x666F;</p>
<p>&#x5176;&#x4E2D;
&#x68CB;&#x5B50;&#xFF1A;&#x62BD;&#x8C61;&#x4EAB;&#x5143;&#x89D2;&#x8272;&#xFF0C;&#x5305;&#x542B;&#x843D;&#x5B50;&#x4E0B;&#x68CB;&#x7684;&#x65B9;&#x6CD5;<br>&#x767D;&#x5B50;&#x3001;&#x9ED1;&#x5B50;&#xFF1A;&#x5177;&#x4F53;&#x4EAB;&#x5143;&#x89D2;&#x8272;<br>&#x843D;&#x5B50;&#x70B9;&#xFF1A;&#x975E;&#x4EAB;&#x5143;&#x89D2;&#x8272;&#xFF08;&#x6307;&#x5B9A;&#x4E86;&#x68CB;&#x5B50;&#x843D;&#x4E0B;&#x7684;&#x4F4D;&#x7F6E;&#xFF09;
&#x4EAB;&#x5143;&#x5DE5;&#x5382;&#x89D2;&#x8272;&#x901A;&#x8FC7;&#x96C6;&#x5408;&#x7BA1;&#x7406;&#x68CB;&#x5B50;&#xFF0C;&#x5E76;&#x63D0;&#x4F9B;&#x83B7;&#x53D6;&#x767D;&#x5B50;&#x3001;&#x9ED1;&#x5B50;&#x7684;&#x65B9;&#x6CD5;
&#x5BA2;&#x6237;&#x89D2;&#x8272;&#x5C31;&#x662F;&#x901A;&#x8FC7;&#x4EAB;&#x5143;&#x5DE5;&#x5382;&#x83B7;&#x53D6;&#x767D;&#x5B50;&#x6216;&#x9ED1;&#x5B50;&#xFF0C;&#x5E76;&#x843D;&#x5B50;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../facademode/" class="navigation navigation-prev " aria-label="Previous page: 外观模式">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../compositemode/" class="navigation navigation-next " aria-label="Next page: 组合模式">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"享元模式","level":"2.1.2.6","depth":3,"next":{"title":"组合模式","level":"2.1.2.7","depth":3,"path":"Design-Patterns/src/main/java/structuredmodel/compositemode/README.md","ref":"Design-Patterns/src/main/java/structuredmodel/compositemode/README.md","articles":[]},"previous":{"title":"外观模式","level":"2.1.2.5","depth":3,"path":"Design-Patterns/src/main/java/structuredmodel/facademode/README.md","ref":"Design-Patterns/src/main/java/structuredmodel/facademode/README.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["expandable-chapters","livereload"],"pluginsConfig":{"expandable-chapters":{},"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"Design-Patterns/src/main/java/structuredmodel/flyweightmode/README.md","mtime":"2019-07-08T11:05:14.987Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-09-09T07:20:35.937Z"},"basePath":"../../../../../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../../../../../gitbook/gitbook.js"></script>
    <script src="../../../../../../gitbook/theme.js"></script>
    
        
        <script src="../../../../../../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="../../../../../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../../../../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../../../../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../../../../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../../../../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../../../../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../../../../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

